﻿using System;
using System;
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{

    class Colietsfortoolholderswithtaperratio1_103
    {
        public static void _Colietsfortoolholderswithtaperratio1_103(Dictionary<string, Object> data)//C-锥柄座
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = fun.SetLayer("contour");//轮廓层
            Layer dimensioning = fun.SetLayer("dimensioning");//尺寸层
            Layer refin = fun.SetLayer("refin");//基准线/内轮廓/虚线
            Layer center = fun.SetLayer("center");//中心线
            Layer textlayer = fun.SetLayer("textlayer");//文本层
            Layer hatching = fun.SetLayer("hatching");//填充层
            Layer additionalline = fun.SetLayer("additionalline");//附加线
            Layer cutting = fun.SetLayer("cutting");//切削层


            //参数
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);

            double scale = Convert.ToDouble(data["scale"]);//放大系数
            //double scale = 5;


            double d2 = Convert.ToDouble(data["d2"]) * scale;
            double d5 = Convert.ToDouble(data["d5"]) * scale;
            double d6 = Convert.ToDouble(data["d6"]) * scale;

            double l7 = Convert.ToDouble(data["l7"]) * scale;
            double l8 = Convert.ToDouble(data["l8"]) * scale;
            double l9 = Convert.ToDouble(data["l9"]) * scale;
            double l10 = Convert.ToDouble(data["l10"]) * scale;


            //坐标
            Vector2 v1 = new Vector2(0, 0);
            Vector2 v2 = new Vector2(v1.X + l9 * 2, v1.Y);
            Vector2 v3 = new Vector2(v2.X - 3, v2.Y);
            Vector2 v4 = new Vector2(v3.X, v3.Y + d5 / 2);
            Vector2 v5 = new Vector2(v4.X - l7, v4.Y);
            Vector2 v6 = new Vector2(v5.X - l7 / 11.306, v5.Y - l7 / 11.306);
            Vector2 v7 = new Vector2(v4.X - l8, v6.Y);
            Vector2 v8 = new Vector2(v7.X, v7.Y + d5 / 10.315);
            Vector2 v9 = new Vector2(v8.X - l9 / 2.34, v8.Y);
            Vector2 v10 = new Vector2(v4.X, v4.Y * -1);
            Vector2 v11 = new Vector2(v5.X, v5.Y * -1);
            Vector2 v12 = new Vector2(v6.X, v6.Y * -1);
            Vector2 v13 = new Vector2(v7.X, v7.Y * -1);
            Vector2 v14 = new Vector2(v8.X, v8.Y * -1);
            Vector2 v15 = new Vector2(v14.X - l9 / 1.2658, v14.Y);
            Vector2 v16 = new Vector2(v3.X, v3.Y + d2 / 2);
            Vector2 v17 = new Vector2(v3.X - l9, v16.Y - l9 * Math.Tan(2.866 * pi / 180));
            Vector2 v28 = new Vector2(v17.X, v3.Y + d6 / 2);
            Vector2 v18 = new Vector2(v28.X - l10, v28.Y);
            Vector2 v19 = new Vector2(v18.X, v3.Y + d6 / 2.305);
            Vector2 v20 = new Vector2(v19.X - l9 / 3.23, v19.Y);
            Vector2 v21 = new Vector2(v16.X, v16.Y * -1);
            Vector2 v22 = new Vector2(v17.X, v17.Y * -1);
            Vector2 v29 = new Vector2(v28.X, v28.Y * -1);
            Vector2 v23 = new Vector2(v18.X, v18.Y * -1);
            Vector2 v24 = new Vector2(v19.X, v19.Y * -1);
            Vector2 v25 = new Vector2(v24.X - l9 / 3.117, v24.Y);
            Vector2 v26 = fun.Polar(v15, 106 * pi / 180, d5 / 5.263);
            Vector2 v27 = fun.Polar(v20, 51 * pi / 180, d5 / 3.764);
            Vector2 v30 = new Vector2(v5.X - (v5.Y - v6.Y) / 2, v5.Y - (v5.Y - v6.Y) / 2);
            Vector2 v31 = new Vector2(v4.X, v30.Y);
            Vector2 v32 = new Vector2(v30.X, v30.Y * -1);
            Vector2 v33 = new Vector2(v31.X, v31.Y * -1);





            //作图
            Line line1 = new Line(v1, v2);
            line1.Layer = center;
            line1.Linetype = Linetype.DashDot;
            line1.LinetypeScale = 3;
            dxf.AddEntity(line1);

            LwPolyline line2 = new LwPolyline();
            line2.Vertexes.Add(new LwPolylineVertex(v27));
            line2.Vertexes.Add(new LwPolylineVertex(v9));
            line2.Vertexes.Add(new LwPolylineVertex(v8));
            line2.Vertexes.Add(new LwPolylineVertex(v7));
            line2.Vertexes.Add(new LwPolylineVertex(v6));
            line2.Vertexes.Add(new LwPolylineVertex(v5));
            line2.Vertexes.Add(new LwPolylineVertex(v4));
            line2.Vertexes.Add(new LwPolylineVertex(v10));
            line2.Vertexes.Add(new LwPolylineVertex(v11));
            line2.Vertexes.Add(new LwPolylineVertex(v12));
            line2.Vertexes.Add(new LwPolylineVertex(v13));
            line2.Vertexes.Add(new LwPolylineVertex(v14));
            line2.Vertexes.Add(new LwPolylineVertex(v15));
            line2.Vertexes.Add(new LwPolylineVertex(v26));
            line2.Vertexes.Add(new LwPolylineVertex(v25));
            line2.Vertexes.Add(new LwPolylineVertex(v20));
            line2.Vertexes.Add(new LwPolylineVertex(v27));
            line2.Vertexes[12].Bulge = Math.Tan(-63 * pi / 180 / 4);
            line2.Vertexes[13].Bulge = Math.Tan(35 * pi / 180 / 4);
            line2.Vertexes[14].Bulge = Math.Tan(-28 * pi / 180 / 4);
            line2.Vertexes[15].Bulge = Math.Tan(-57 * pi / 180 / 4);
            fun.PolyFill(dxf, line2, 0.5, contour);

            LwPolyline line3 = Pus.AddPointsToLwPolyLine(v18, v28, v29, v23, v18);
            fun.PolyFill(dxf, line3, 0.5, contour);

            LwPolyline line4 = Pus.AddPointsToLwPolyLine(v22, v17, v16, v21, v22);
            fun.PolyFill(dxf, line4, 0.5, contour);

            LwPolyline line5 = new LwPolyline();
            line5.Vertexes.Add(new LwPolylineVertex(v20));
            line5.Vertexes.Add(new LwPolylineVertex(v19));
            line5.Vertexes.Add(new LwPolylineVertex(v24));
            line5.Vertexes.Add(new LwPolylineVertex(v25));
            fun.PolyFill(dxf, line5, 0.5, contour);

            //LwPolyline line6 = new LwPolyline();
            //line6.Vertexes.Add(new LwPolylineVertex(v30));
            //line6.Vertexes.Add(new LwPolylineVertex(v31));
            //line6.Vertexes.Add(new LwPolylineVertex(v33));
            //line6.Vertexes.Add(new LwPolylineVertex(v32));
            //fun.PolyFill(dxf, line6, 0.5, contour);





            //填充
            LwPolyline hatchpoly1 = new LwPolyline();
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v27));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v20));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v19));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v18));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v28));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v17));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v16));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v4));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v5));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v6));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v7));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v8));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v9));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v27));
            hatchpoly1.Vertexes[0].Bulge = Math.Tan(-57 * pi / 180 / 4);
            hatchpoly1.IsClosed = true;
            String[] htype1 = new String[100];
            Line[] hline1 = new Line[100];
            Arc[] harc1 = new Arc[100];
            int hnumst1 = hatchpoly1.Explode().Count;  //记录爆炸后，线段有多少
            for (int i = 0; i < hnumst1; i++)
            {
                htype1[i] = hatchpoly1.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
                if (htype1[i] == "Line")
                {
                    hline1[i] = (Line)hatchpoly1.Explode()[i];
                }
                if (htype1[i] == "Arc")
                {
                    harc1[i] = (Arc)hatchpoly1.Explode()[i];
                }
            }
            fun.hatchsub1(dxf, htype1, hnumst1, 45, 10, hline1, harc1, hatching);

            LwPolyline hatchpoly2 = new LwPolyline();
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v15));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v26));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v25));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v24));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v23));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v29));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v22));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v21));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v10));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v11));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v12));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v13));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v14));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v15));
            hatchpoly2.Vertexes[0].Bulge = Math.Tan(-64 * pi / 180 / 4);
            hatchpoly2.Vertexes[1].Bulge = Math.Tan(35 * pi / 180 / 4);
            hatchpoly2.IsClosed = true;
            String[] htype2 = new String[100];
            Line[] hline2 = new Line[100];
            Arc[] harc2 = new Arc[100];
            int hnumst2 = hatchpoly2.Explode().Count;  //记录爆炸后，线段有多
                                                       //
                                                       //少
            for (int i = 0; i < hnumst2; i++)
            {
                htype2[i] = hatchpoly2.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
                if (htype2[i] == "Line")
                {
                    hline2[i] = (Line)hatchpoly2.Explode()[i];
                }
                if (htype2[i] == "Arc")
                {
                    harc2[i] = (Arc)hatchpoly2.Explode()[i];
                }
            }
            fun.hatchsub1(dxf, htype2, hnumst2, 45, 10, hline2, harc2, hatching);


            Line linea1 = new Line(v30, v31);
            Line linea2 = new Line(v32, v33);


            //标注

            double height = 3.5;
            double arrowsize = height;
            double exline = height / 2;
            double offset = 10;

            DimensionStyle ds = fun.DimStyle(true);




            fun.dimrot1(dxf, v28, v4, 0, offset + (v8.Y - (v28.Y + v4.Y) / 2), 1, height, arrowsize, exline, "l9=" + l9 / scale, "", "");

            fun.dimrot1(dxf, v18, v28, 0, offset + (v8.Y - (v28.Y + v18.Y) / 2), 1, height, arrowsize, exline, "l10=" + l10 / scale, "", "");

            //fun.dimrot1(dxf, v16, v21, -90, offset, 1, height, arrowsize, exline, "d2=" + d2, "", "");

            LinearDimension d = new LinearDimension(v16, v21, offset, -90, ds);
            d.UserText = "d2=%%C" + d2 / scale;
            dxf.AddEntity(d);

            fun.dimrot1(dxf, v4, v10, -90, offset * 2, 1, height, arrowsize, exline, "d5=%%C" + d5 / scale, "", "");

            fun.dimrot1(dxf, v18, v23, 90, offset + v23.X - v26.X, 1, height, arrowsize, exline, "d6=%%C" + d6 / scale, "", "");

            fun.jtbz(dxf, (v16 + v17) / 2, -120, offset, offset, "1:10", height, height);
            //fun.xwgcbz(dxf, new Vector2(v10.X + offset * 2, v10.Y), offset, offset, -90, 0, "ytd", "0.01", "A");



            double isframe = Convert.ToInt32(data["isframe"]);
            String isframe_chose = Convert.ToString(data["isframe_chose"]);
            Vector2 p0 = new Vector2(0, 0);
            if (isframe == 1)
            {
                if (isframe_chose.Equals("0"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA0X(dxf, new Vector2(p0.X - 1189 / 2.0, p0.Y + 841 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("1"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA1X(dxf, new Vector2(p0.X - 841 / 2.0, p0.Y + 594 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("2"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA2X(dxf, new Vector2(p0.X - 594 / 2.0, p0.Y + 420 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("3"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 420 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4X"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2(p0.X - 297 / 2.0, p0.Y + 210 / 2.0 - 10 * 2.26), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4Y"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 210 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }
            }

            fun.AddEntities(dxf, additionalline, linea1, linea2);

            fun.SetLayer(dxf); fun.SetByLayer(dxf);




            //定义格式，路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);


        }

    }
}

